package com.example.mydy.withdraw;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.mydy.MainActivity;
import com.example.mydy.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class WithdrawStep4Fragment extends Fragment implements WithdrawStepListener {

    private ImageView ivStatusIcon;
    private TextView tvStatusTitle;
    private TextView tvStatusDesc;
    private TextView tvWithdrawId;
    private TextView tvResultAmount;
    private TextView tvApplyTime;
    private TextView tvResultAlipayAccount;
    private Button btnWithdrawHistory;
    private Button btnBackToHome;
    
    private WithdrawDataManager dataManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_withdraw_step4, container, false);
        
        // 初始化视图
        ivStatusIcon = view.findViewById(R.id.iv_status_icon);
        tvStatusTitle = view.findViewById(R.id.tv_status_title);
        tvStatusDesc = view.findViewById(R.id.tv_status_desc);
        tvWithdrawId = view.findViewById(R.id.tv_withdraw_id);
        tvResultAmount = view.findViewById(R.id.tv_result_amount);
        tvApplyTime = view.findViewById(R.id.tv_apply_time);
        tvResultAlipayAccount = view.findViewById(R.id.tv_result_alipay_account);
        btnWithdrawHistory = view.findViewById(R.id.btn_withdraw_history);
        btnBackToHome = view.findViewById(R.id.btn_back_to_home);
        
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // 获取数据管理器
        dataManager = WithdrawDataManager.getInstance();
        
        // 加载结果信息
        loadResultData();
        
        // 设置按钮点击事件
        btnWithdrawHistory.setOnClickListener(v -> {
            // 跳转到提现记录页面
            Intent intent = new Intent(requireActivity(), WithdrawalHistoryActivity.class);
            startActivity(intent);
        });
        
        btnBackToHome.setOnClickListener(v -> {
            // 返回首页
            Intent intent = new Intent(requireActivity(), MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            requireActivity().finish();
        });
    }

    private void loadResultData() {
        // 设置状态信息
        tvStatusTitle.setText("提现申请已提交");
        tvStatusDesc.setText("您的提现申请已成功提交，正在等待审核，预计1-3个工作日内到账");
        
        // 设置提现详情
        int withdrawalId = dataManager.getWithdrawalId();
        tvWithdrawId.setText("WD" + String.format("%09d", withdrawalId));
        
        double amount = dataManager.getWithdrawAmount();
        tvResultAmount.setText(String.format("¥ %.2f", amount));
        
        // 设置申请时间
        String withdrawalTime = dataManager.getWithdrawalTime();
        if (withdrawalTime.isEmpty()) {
            // 如果没有设置时间，使用当前时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            withdrawalTime = sdf.format(new Date());
        }
        tvApplyTime.setText(withdrawalTime);
        
        // 设置支付宝账号
        String alipayAccount = dataManager.getAlipayAccount();
        if (alipayAccount.length() > 7) {
            String maskedAccount = alipayAccount.substring(0, 3) + "****" + 
                    alipayAccount.substring(alipayAccount.length() - 4);
            tvResultAlipayAccount.setText(maskedAccount);
        } else {
            tvResultAlipayAccount.setText(alipayAccount);
        }
    }

    @Override
    public boolean validate() {
        // 最后一步，无需验证
        return true;
    }
} 